package com.applet.applet_shangcheng.controller;


import com.applet.applet_shangcheng.entity.Admin;
import com.applet.applet_shangcheng.entity.User;
import com.applet.applet_shangcheng.utlis.Result;
import com.applet.applet_shangcheng.utlis.StatusCode;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

//登录页面
@Controller
@Api(value="登录",tags={"登录"})
public class HomeController {
    @RequestMapping(path = "/index", method = RequestMethod.GET)
    public String getIndexPage(Model model) {
        // 认证成功后,结果会通过SecurityContextHolder存入SecurityContext中.
        Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (obj instanceof User) {
            model.addAttribute("loginUser", obj);
        }
        return "/index";
    }

    @RequestMapping(path = "/getUser", method = RequestMethod.GET)
    @ResponseBody
    public Result getUser(Model model) {
        // 认证成功后,结果会通过SecurityContextHolder存入SecurityContext中.
        Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (obj instanceof User) {
            model.addAttribute("loginUser", obj);
            Admin admin = new Admin();
            String username = ((User) obj).getUsername();
            int status = ((User) obj).getStatus();
            String image = ((User) obj).getImage();
            admin.setUsername(username);
            admin.setStatus(status);
            admin.setImage(image);
            return new Result(true, StatusCode.OK, "获取成功", admin);
        }
        return new Result(false, StatusCode.ERROR, "获取失败", obj);
    }

    @RequestMapping(path = "/goods", method = RequestMethod.GET)
    public String getDiscussPage() {
        return "site/goods";
    }

    @RequestMapping(path = "/message", method = RequestMethod.GET)
    public String getMessagePage() {
        return "site/message";
    }

    @RequestMapping(path = "/senior", method = RequestMethod.GET)
    public String getLetterPage() {
        return "site/senior";
    }

    @RequestMapping(path = "/order", method = RequestMethod.GET)
    public String getOrderPage() {
        return "site/order";
    }

    @RequestMapping(path = "/admin", method = RequestMethod.GET)
    public String getAdminPage() {
        return "site/admin";
    }

    //@RequestMapping(path = "/loginpage", method = {RequestMethod.GET, RequestMethod.POST})
    @RequestMapping(path = "/loginpage", method = {RequestMethod.GET, RequestMethod.POST})
    public String getLoginPage() {
        return "index";
    }

    // 拒绝访问时的提示页面
    @RequestMapping(path = "/denied", method = RequestMethod.GET)
    public String getDeniedPage() {
        return "error/404";
    }

    // 获取用户信息
    @RequestMapping(path = "/getMenuList", method = RequestMethod.GET)
    @ResponseBody
    public Result getMenuList() {
        // 认证成功后,结果会通过SecurityContextHolder存入SecurityContext中.
        Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
/*if (obj instanceof User) {
            int status = ((User) obj).getStatus();
        if (status==0){
            LinkedHashMap map = new LinkedHashMap();
            map.put(path, )
            return new Result(true, StatusCode.OK,"获取成功");
        }
        }*/
        return new Result(false, StatusCode.ERROR, "获取失败");
    }

    public static void main(String[] args) {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        ObjectMapper objectMapper = new ObjectMapper();
        map.put("path", "1");
        map.put("title", "工作台");
        map.put("icon", "fa-dashboard");
        Map<String, Object> map1 = new HashMap<>();
        map1.put("path", "/1-1");
        map1.put("title", "工作管理");
        map1.put("linkUrl", "report_member.html");
        List<String> list1 = new LinkedList<>();
        map1.put("children", list1);
        List<Map<String, Object>> list2 = new ArrayList<>();
        list2.add(map1);
        map.put("children", list2);
        list.add(map);
        try {
            String value = objectMapper.writeValueAsString(list);
            try {
                String value1 = objectMapper.writeValueAsString(list);
                System.out.println(value1);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

}

//ticket